<?php
/**属性
 * Class AttrController
 * @package Admin\Controller
 * @author leiying
 */

namespace Admin\Controller;

class AttrController extends AdminController {

    /**属性列表
     * @author leiying
     */
    public function attrList(){
        if(I('post.btn')){$this->success('');}
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_attrlist',$get);
        }else{
            if(I('get.p')){
                $this->assign('p',I('get.p'));
            }else{
                session('searchdata_attrlist',null);
            }
            $get = $search_data = session('searchdata_attrlist');
        }
        $type_id = I('type_id');
        if($type_id != '' && isset($type_id)){
            $search_data['type_id'] = $type_id;
            $where['a.type_id'] = array('eq',$type_id);
        }


        if(isset($get['attr_name']) && $get['attr_name'] != ''){
            $search_data['attr_name'] = $get['attr_name'];
            $where['a.attr_name'] = array('like',"%".$get['attr_name']."%");
        }

        $count = M('attribute')->alias('a')->where($where)->count();

        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数

        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出

        $this->assign('page',$show);// 赋值分页输出



        $lists = M('attribute')
            ->alias("a")
            ->field("a.*,b.type_name")
            ->join(C('DB_PREFIX')."goods_type b on a.type_id=b.id",'left')
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order("a.sort asc")
            ->select();
        foreach($lists as &$v){
            $v["attr_values"] = str_replace(array("\r\n","\r","\n"),",",$v["attr_values"]);
        }
        $this->assign('lists',$lists);

        $typelist = M("goods_type")->field("type_name,id type_id")->select();
        $this->assign('typelist',$typelist);
        $p = I('get.p',"1");
        $this->assign("p",$p);

        $this->assign('searchs',$search_data);

        $this->display();

    }

    /**添加属性
     * @author leiying
     */
    public function attrAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            //处理参数
            $type_id = $post['type_id'];
            if(empty($type_id)){
                $this->error('请选择需要添加属性的宝贝类型');
            }
            $attr_name = trim($post["attr_name"]);
            if($attr_name == ''){
                $this->error('属性名称不能为空');
            }
            $attr_input_type = $post["attr_input_type"];
            if(!in_array($attr_input_type,array(0,1))){
                $this->error('请选择录入方式');
            }
            $attr_select_type = $post["attr_select_type"];
            if(!in_array($attr_select_type,array(1,2))){
                $this->error('请选择选择方式');
            }

            $attr_values = trim($post["attr_values"]);
            $sort = empty($post["sort"])?0:intval($post["sort"]);

            $data = array(
                'type_id'=>$type_id,
                'attr_name'=>$attr_name,
                'attr_input_type'=>$attr_input_type,
                'attr_values'=>$attr_values,
                'attr_select_type'=>$attr_select_type,
                'sort'=>$sort
            );
            $res = M("attribute")->add($data);
            if($res){
                $this->success("添加属性成功");
            }else{
                $this->error("添加属性失败");
            }
        }else{
            $type_id = I('get.type_id');
            $this->assign("type_id",$type_id);
            $this->display();
        }
    }

    /**根据分类id获取属性
     * @author leiying
     */
    public function get_child_attr(){
        if(IS_POST){
            $post = I("post.");
            $type_id = $post["type_id"];
            $goods_id = $post["goods_id"];
            $list = $this->get_attr_list($type_id,$goods_id);
            $this->success($list);
        }else{
            $this->error("非法操作");
        }
    }
    /**取得通用属性和某分类的属性，以及某宝贝的属性值
     * @param $type_id    分类编号
     * @param int $goods_id 宝贝编号
     * @return  array   规格与属性列表
     * @author leiying
     */
    public function get_attr_list($type_id,$goods_id = 0){
        if (empty($type_id))
        {
            return array();
        }
        $row = M("attribute")
            ->field("attr_id,attr_name,attr_input_type,attr_values,attr_select_type")
            ->where(array("type_id"=>$type_id))
            ->order("sort asc,attr_id desc")
            ->select();

        if($row){
            foreach($row as &$v){
                if($v["attr_input_type"] == 0){//手工录入
                    $v["attr_values"] = '';
                }
                if(empty($v["attr_values"])){
                    $v["attr"] = array();
                }else{
                    $attr_values = str_replace(array("\r\n","\r","\n"),",",$v["attr_values"]);
                    $v["attr"] = explode(',',$attr_values);
                }

                unset($v["attr_values"]);
            }
            $data = $row;
        }else{
            $data = array();
        }
        return $data;
    }


    /**编辑属性
     * @author leiying
     */
    public function attrEdit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            $id = $post['attr_id'];
            if(empty($id)){
                $this->error("请选择需要操作的数据");
            }
            //处理参数
            $type_id = $post['type_id'];
            if(empty($type_id)){
                $this->error('请选择需要添加属性的宝贝类型');
            }
            $attr_name = trim($post["attr_name"]);
            if($attr_name == ''){
                $this->error('属性名称不能为空');
            }
            $attr_input_type = $post["attr_input_type"];
            if(!in_array($attr_input_type,array(0,1))){
                $this->error('请选择录入方式');
            }
            $attr_select_type = $post["attr_select_type"];
            if(!in_array($attr_select_type,array(1,2))){
                $this->error('请选择选择方式');
            }

            $attr_values = trim($post["attr_values"]);
            $sort = empty($post["sort"])?0:intval($post["sort"]);

            $data = array(
                'type_id'=>$type_id,
                'attr_name'=>$attr_name,
                'attr_input_type'=>$attr_input_type,
                'attr_values'=>$attr_values,
                'attr_select_type'=>$attr_select_type,
                'sort'=>$sort
            );
            $res = M('attribute')->where(array('attr_id'=>$id))->save($data);
            if(is_numeric($res)){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{
            $id = I('get.id');
            if($id){
                $type_id = I('get.type_id');
                $this->assign("type_id",$type_id);
                $p = I('get.p',1);
                $this->assign('p',$p);
                $data = M("attribute")->where(array("attr_id"=>$id))->find();
                $this->assign("data",$data);
                $this->display();
            }else{$this->error("请先选择操作数据");}

        }
    }


    /**删除属性
     * @author leiying
     */
    public function deleteAttr(){
        if(IS_POST){
            $id = I('post.id');
            if(empty($id)){
                $this->error("请选择需要操作的数据");
            }

            $res = M('attribute')->where(array('attr_id'=>$id))->delete();
            if($res){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }else{
            $this->error('非法操作');
        }
    }

}
